1693B - Fake Plastic Trees - CodeForces Solution


dfs and similar dp greedy trees *1700

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int NMAX=2e5+5;

vector<int>v[NMAX];
int t[NMAX];
int l[NMAX];
int r[NMAX];
long long dp[NMAX];

int kon;

void dfs(int p,int tata)
{
    dp[p]=0;
    for(auto i:v[p])
    {
        if(i!=tata)
        {
            dfs(i,p);
            dp[p]+=dp[i];
        }
    }
    if(dp[p]>r[p])
        dp[p]=r[p];
    else
    {
        if(dp[p]<l[p])
        {
            dp[p]=r[p];
            kon++;
        }
    }
}

int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        int n,m,i,j;
        kon=0;
        cin>>n;
        for(i=1;i<=n;i++)
            v[i].clear();
        for(i=2;i<=n;i++)
        {
            cin>>t[i];
            v[t[i]].push_back(i);
        }
        for(i=1;i<=n;i++)
            cin>>l[i]>>r[i];
        dfs(1,0);
        cout<<kon<<"\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam